/*
 * ProjetoPSA - Plano de Substituição de Aulas
 *    Copyright (C) 2010  Marcelo e Cicero
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projetoPSA.facades;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;
import projetoPSA.hibernate.DaoGenerico;
import projetoPSA.hibernate.HibernateUtils;
import projetoPSA.pojos.Sala;

/**
 *
 * @author Marcelo e Cicero
 */
public class SalaFacade {

    private Sala sala;
    private Session sessao;
    private Criteria criteria;



    public SalaFacade(Sala sala) {
        this.sala = sala;
    }

    public SalaFacade() {
        begin();
    }

    private void begin(){
        sessao = HibernateUtils.getSessao();
        criteria = sessao.createCriteria(Sala.class);
        sessao.beginTransaction().begin();
    }
    private void commit(){
        sessao.beginTransaction().commit();
        sessao.flush();
        sessao.close();
    }

    public List<Sala> getSalas() {
        begin();
        List ret = criteria.list();
        commit();
        return ret;
    }

    public Sala getSala(Long id) {
        begin();
        Sala ret = (Sala) criteria.add(Expression.eq("id", id)).uniqueResult();
        commit();
        return ret;
    }
    public Sala getSala(String sigla) {
        begin();
        Sala ret = (Sala) criteria.add(Expression.eq("sigla", sigla)).uniqueResult();
        commit();
        return ret;
    }

    public void persiste() {
        begin();
        sessao.saveOrUpdate(sala);
        commit();
    }

    public void delete() {
        begin();
        sessao.delete(sala);
        commit();
        
    }
}
